package leecode

import "sort"

func threeSumLCR(nums []int) (result [][]int) {
	sort.Ints(nums)
	var left, right int
	for i := 0; i < len(nums); i++ {
		if nums[i] > 0 {
			return
		}
		left = i + 1
		right = len(nums) - 1
		// a + b + c
		//首先对a去重
		if i > 0 && nums[i] == nums[i-1] {
			continue
		}
		for left < right {
			if nums[i]+nums[left]+nums[right] > 0 {
				//大于0 说明right过大
				right--
			} else if nums[i]+nums[left]+nums[right] < 0 {
				left++
			} else {
				result = append(result, []int{nums[i], nums[left], nums[right]})
				//对b , c 去重
				for left < right && nums[left] == nums[left+1] {
					left++
				}
				for left < right && nums[right] == nums[right-1] {
					right--
				}
				left++
				right--
			}
		}
	}
	return
}
